package com.jiesen.base.auth;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;


/**
* WebSocket 控制类
*
**/
//@Slf4j
//@Controller
//@RequestMapping("/webSocketCtrl")
@ServerEndpoint("/logUtil")
@Slf4j
@RestController
public class WebSocketController {

//	@RequestMapping(value = "/helloWebSocket",method = RequestMethod.GET)
//	public String helloWebSocket(){
//		return "helloWebSocket";
//	}
//	
//	@ResponseBody
//	@RequestMapping(value = "/pushMessageToWeb", method = RequestMethod.POST, consumes = "application/json")
//	public String pushMessageToWeb(@RequestBody String message) {
//		try {
//			WebSocketServer.sendInfo("有新内容：" + message);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return message;
//	}
// 
//	@RequestMapping("/hello")
//	public String helloHtml(HashMap<String, Object> map) {
//		map.put("hello", "这是一个thymeleaf页面");
//		return "MyHtml";
//	}
//	@RequestMapping("/hello1")
//	public String helloHtml1(HashMap<String, Object> map) {
//		map.put("hello", "这是一个thymeleaf页面");
//		return "MyHtml2";
//	}
	
	
	
	private static Session session;

    @OnOpen
    public void onOpen(Session session) {
        WebSocketController.session = session;
    }

    @OnClose
    public void onClose() {

    }

    @OnMessage
    public void onMessage(String message, Session session) {
        try {
            sendMessage(message+"：成功收到");
        } catch (Exception e) {
            log.error("发送异常");
        }
    }

    @OnError
    public void onError(Session session, Throwable error) {
    }

    public static void sendMessage(String message) throws Exception {
    	WebSocketServer.sendInfo(message);
        if (session!=null){
            if (WebSocketController.session.isOpen()) {
                WebSocketController.session.getBasicRemote().sendText(message);
            }
        }
    }


}
